package com.atguigu.realtime.demos

import java.util

import com.alibaba.fastjson.{JSON, JSONObject}
import com.google.gson.Gson

/**
 * Created by Smexy on 2022/8/26
 */
object StartLogParseDemo {

  def main(args: Array[String]): Unit = {

    val gson = new Gson()

    val str =
      """
        |
        |{
        |"common":{   "ar":"420000","ba":"Redmi","ch":"xiaomi","is_new":"0",
        |            "md":"Redmi k30","mid":"mid_32","os":"Android 11.0","uid":"783","vc":"v2.1.134"
        |          },
        |"start":{"entry":"icon","loading_time":15738,"open_ad_id":19,"open_ad_ms":1892,"open_ad_skip_ms":1441},
        |"ts":1661324216000}
        |""".stripMargin


    //把字符串转为Map
    val map: JSONObject = JSON.parseObject(str)
    val commonStr: String = map.getString("common")
    //把common转为map
    val commonMap: JSONObject = JSON.parseObject(commonStr)
    //取出真正存放数据的Map
    val dataMap: util.Map[String, AnyRef] = commonMap.getInnerMap
    //将ts放进去
    dataMap.put("ts",map.getString("ts"))

    //放入start部分
    val startMap: util.Map[String, AnyRef] = JSON.parseObject(map.getString("start")).getInnerMap
    //将两个map中的数据合并
    dataMap.putAll(startMap)

    println(gson.toJson(dataMap))


  }

}
